#! /usr/bin/env node
import { readFileSync } from "fs";
import { join } from "path";
import { merge } from "./main";
import { getArgv } from "./utils/ArgvUtil";

const command = process.argv;

if (command[2] == '-h' || command[2] == '-help') {
    showHelpStr();
} else if (command[2] == '-v' || command[2] == '-version') {
    let verUrl = join(__dirname, '../package.json');
    let verJson = JSON.parse(readFileSync(verUrl).toString())
    console.log('version:', verJson.version);
} else {
    if (command.length > 3) {
        let argv = getArgv(command);
        console.log(argv);

        let path = process.cwd();
        let inputPath = argv['-i'];
        let outPath = argv['-o'];
        let exts = argv['-e'] || argv['-ext'] || [];
        let prefix = argv['-p']
        let fileName = argv['-n']
        let del = argv['-d']
        let size = argv['-s']
        let outFileType = argv['-ot'];// 输出文件的类型,不填默认为json

        let ipath = inputPath ? inputPath[0] : path;
        let opath = outPath ? outPath[0] : ipath;
        let pf = prefix ? prefix[0] : ''
        let fn = fileName ? fileName[0] : 'd3file.json'
        let delFile = del ? del[0] : 'false';
        let outType = outFileType ? outFileType[0] : 'json';
        let fileSize = size ? +(size[0]) : Number.MAX_SAFE_INTEGER;
        merge(ipath, exts, opath, pf, fn, delFile, outType, fileSize);
    } else {
        showHelpStr();
    }
}

function showHelpStr() {
    console.log("==> thank you for using laya3dm <==");
    console.log(`no "${command[2]}" cmd`);
    console.log("if you have question, you can mail me. mail: bfstiger@gmail.com");
    console.log("usage:");

    console.log(`into you want fileMerge dir, input: laya3dm`);
}

